#include "alarmeventdelegate.h"

AlarmEventDelegate::AlarmEventDelegate(QObject *parent)
    : QStyledItemDelegate(parent) {
}

QWidget* AlarmEventDelegate::createEditor(QWidget *parent,
                                         const QStyleOptionViewItem &option,
                                         const QModelIndex &index) const {
    if(index.column() == 0) // 时间列
        return createTimeEditor(parent);
//    else if(index.column() == 1) // 重复规则列
//        return createRepeatEditor(parent);
//    else if(index.column() == 2) // 状态列
//        return createStateEditor(parent);

    return QStyledItemDelegate::createEditor(parent, option, index);
}

void AlarmEventDelegate::setEditorData(QWidget *editor,
                                      const QModelIndex &index) const {
    QVariant value = index.model()->data(index, Qt::DisplayRole);



    if(QTimeEdit *timeEdit = qobject_cast<QTimeEdit*>(editor)) {
        timeEdit->setTime(value.toTime());
    }
}

void AlarmEventDelegate::setModelData(QWidget *editor,
                                     QAbstractItemModel *model,
                                     const QModelIndex &index) const {
    if(QTimeEdit *timeEdit = qobject_cast<QTimeEdit*>(editor)) {
        model->setData(index, timeEdit->time(), Qt::EditRole);
    }
    else if(QLineEdit *lineEdit = qobject_cast<QLineEdit*>(editor)) {
        model->setData(index, lineEdit->text(), Qt::EditRole);
    }
}

void AlarmEventDelegate::updateEditorGeometry(QWidget *editor,
                                             const QStyleOptionViewItem &option,
                                             const QModelIndex &index) const {
    editor->setGeometry(option.rect);
}

QWidget* AlarmEventDelegate::createTimeEditor(QWidget *parent) const {
    QTimeEdit *editor = new QTimeEdit(parent);
    editor->setDisplayFormat("HH:mm");
    editor->setMinimumTime(QTime(0,0));
    editor->setMaximumTime(QTime(23,59));
    return editor;
}

QWidget* AlarmEventDelegate::createRepeatEditor(QWidget *parent) const {
    QCheckBox *checkBox = new QCheckBox(parent);
    checkBox->setTristate(false);
    return checkBox;
}

QWidget* AlarmEventDelegate::createStateEditor(QWidget *parent) const {
    QCheckBox *checkBox = new QCheckBox(parent);
    checkBox->setTristate(false);
    return checkBox;
}
